//---------------------------------------------------------------------
// This file is part of the Microsoft BizTalk Server 2006 SDK
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// This source code is intended only as a supplement to Microsoft BizTalk
// Server 2006 release and/or on-line documentation. See these other
// materials for detailed information regarding Microsoft code samples.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
//---------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace MsPartners.Unisys.BizTalkAdapters.AdapterWizard
{
	public class WzPageGeneralSetup : Microsoft.BizTalk.Wizard.WizardInteriorPage, IWizardControlInterface
	{
		private const string transportRegEx = @"^[_a-zA-Z][_a-zA-Z0-9]*$";
		private const string propNamespaceRegEx = @"^[Hh][Tt][Tt][Pp]://([-:/_.a-zA-Z0-9]*$)";
		public event AddWizardResultEvent addWizardResultEvent;
		private System.Windows.Forms.CheckBox chkRecvAdapter;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox txtTransportType;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox txtPropNameSpace;
		private System.Windows.Forms.CheckBox chkTransmitAdapter;
		private System.Windows.Forms.ErrorProvider ErrProv;
		private System.ComponentModel.IContainer components = null;

		public WzPageGeneralSetup()
		{
			// This call is required by the Windows Form Designer.
			InitializeComponent();

			// TODO: Add any initialization after the InitializeComponent call
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		protected void AddWizardResult(string strName,string strValue)
		{
			PropertyPairEvent PropertyPair = new PropertyPairEvent(strName,strValue);
			OnAddWizardResult(PropertyPair);
		}

		/// <summary>
		/// The protected OnRaiseProperty method raises the event by invoking 
		/// the delegates. The sender is always this, the current instance 
		/// of the class.
		/// </summary>
		/// <param name="e"></param>
		protected virtual void OnAddWizardResult(PropertyPairEvent e)
		{
			if (e != null) 
			{
				// Invokes the delegates. 
				addWizardResultEvent(this,e);
			}
		}

		/// <summary>
		/// Returns true if all fields are correctly entered
		/// </summary>
		/// <returns></returns>
		private bool GetAllStates()
		{
			return (Regex.IsMatch(txtTransportType.Text,transportRegEx) &&
				Regex.IsMatch(txtPropNameSpace.Text,propNamespaceRegEx) &&
				(chkRecvAdapter.Checked || chkTransmitAdapter.Checked));
		}

		private void WzPageGeneralSetup_Leave(object sender, System.EventArgs e)
		{
			try
			{
				AddWizardResult(WizardValues.TransportType,txtTransportType.Text);
				AddWizardResult(WizardValues.PropertyNamespace,txtPropNameSpace.Text);
				AddWizardResult(WizardValues.AdapterIsReceiveAdapter,chkRecvAdapter.Checked.ToString());
				AddWizardResult(WizardValues.AdapterIsTransmitAdapter,chkTransmitAdapter.Checked.ToString());
			}
			catch(Exception err)
			{
				MessageBox.Show(err.Message);
				Trace.WriteLine(err.Message + Environment.NewLine + err.StackTrace);
			}
		}

		private void txtTransportType_Validating(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if (!Regex.IsMatch(txtTransportType.Text,transportRegEx) && txtTransportType.Text.Length > 0)
			{
				ErrProv.SetError(txtTransportType,
					"TransportType must start with a non-alphanumeric character");
			}
			else
			{
				ErrProv.SetError(txtTransportType,"");
				EnableNext(GetAllStates());	
			}		
		}

		private void txtPropNameSpace_Validating(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if (!Regex.IsMatch(txtPropNameSpace.Text,propNamespaceRegEx) && txtPropNameSpace.Text.Length > 0)
			{
				ErrProv.SetError(txtPropNameSpace,
					"Property namespace must be in URL format (e.g. http://mynamespace)");
			}
			else
			{
				ErrProv.SetError(txtPropNameSpace,"");
				EnableNext(GetAllStates());	
			}
		}

		private void chkRecvAdapter_CheckedChanged(object sender, System.EventArgs e)
		{
			EnableNext(GetAllStates());
		}

		private void chkTransmitAdapter_CheckedChanged(object sender, System.EventArgs e)
		{
			EnableNext(GetAllStates());
		}

		#region Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(WzPageGeneralSetup));
			this.chkRecvAdapter = new System.Windows.Forms.CheckBox();
			this.chkTransmitAdapter = new System.Windows.Forms.CheckBox();
			this.label1 = new System.Windows.Forms.Label();
			this.txtTransportType = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.txtPropNameSpace = new System.Windows.Forms.TextBox();
			this.ErrProv = new System.Windows.Forms.ErrorProvider(this.components);
			this.panelHeader.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.ErrProv)).BeginInit();
			this.SuspendLayout();
			// 
			// labelTitle
			// 
			resources.ApplyResources(this.labelTitle, "labelTitle");
			// 
			// labelSubTitle
			// 
			resources.ApplyResources(this.labelSubTitle, "labelSubTitle");
			// 
			// chkRecvAdapter
			// 
			resources.ApplyResources(this.chkRecvAdapter, "chkRecvAdapter");
			this.chkRecvAdapter.Name = "chkRecvAdapter";
			this.chkRecvAdapter.CheckedChanged += new System.EventHandler(this.chkRecvAdapter_CheckedChanged);
			// 
			// chkTransmitAdapter
			// 
			resources.ApplyResources(this.chkTransmitAdapter, "chkTransmitAdapter");
			this.chkTransmitAdapter.Name = "chkTransmitAdapter";
			this.chkTransmitAdapter.CheckedChanged += new System.EventHandler(this.chkTransmitAdapter_CheckedChanged);
			// 
			// label1
			// 
			resources.ApplyResources(this.label1, "label1");
			this.label1.Name = "label1";
			// 
			// txtTransportType
			// 
			resources.ApplyResources(this.txtTransportType, "txtTransportType");
			this.txtTransportType.Name = "txtTransportType";
			this.txtTransportType.Validating += new System.ComponentModel.CancelEventHandler(this.txtTransportType_Validating);
			// 
			// label2
			// 
			resources.ApplyResources(this.label2, "label2");
			this.label2.Name = "label2";
			// 
			// txtPropNameSpace
			// 
			resources.ApplyResources(this.txtPropNameSpace, "txtPropNameSpace");
			this.txtPropNameSpace.Name = "txtPropNameSpace";
			this.txtPropNameSpace.Validating += new System.ComponentModel.CancelEventHandler(this.txtPropNameSpace_Validating);
			// 
			// ErrProv
			// 
			this.ErrProv.ContainerControl = this;
			resources.ApplyResources(this.ErrProv, "ErrProv");
			// 
			// WzPageGeneralSetup
			// 
			this.Controls.Add(this.label2);
			this.Controls.Add(this.txtPropNameSpace);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.txtTransportType);
			this.Controls.Add(this.chkRecvAdapter);
			this.Controls.Add(this.chkTransmitAdapter);
			this.Name = "WzPageGeneralSetup";
			this.SubTitle = "Specify generic adapter properties";
			this.Title = "General setup";
			this.Load += new System.EventHandler(this.WzPageGeneralSetup_Load);
			this.Leave += new System.EventHandler(this.WzPageGeneralSetup_Leave);
			this.Controls.SetChildIndex(this.chkTransmitAdapter, 0);
			this.Controls.SetChildIndex(this.chkRecvAdapter, 0);
			this.Controls.SetChildIndex(this.panelHeader, 0);
			this.Controls.SetChildIndex(this.txtTransportType, 0);
			this.Controls.SetChildIndex(this.label1, 0);
			this.Controls.SetChildIndex(this.txtPropNameSpace, 0);
			this.Controls.SetChildIndex(this.label2, 0);
			this.panelHeader.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.ErrProv)).EndInit();
			this.ResumeLayout(false);
			this.PerformLayout();

		}
		#endregion

		private void WzPageGeneralSetup_Load(object sender, EventArgs e)
		{
#if DEBUG
			txtPropNameSpace.Text = "http://test1";
			txtTransportType.Text = "test1";
			chkRecvAdapter.Checked = true;
			chkTransmitAdapter.Checked = true;
			txtPropNameSpace.Select();
#endif
		}

		#region IWizardControlInterface Members

		bool IWizardControlInterface.NextButtonEnabled
		{
			get { return GetAllStates(); }
		}

		bool IWizardControlInterface.NeedSummary
		{
			get { return false; }
		}

		#endregion
	}
}

